//
// Created by zouzheng on 2020/12/22.
//
#include "Code.h"
#include <stdio.h>
#include <Type.h>
#include <GlobalVar.h>
#include "Str.h"

Type CodeType = {
        .type=&TypeType,
    .name="Type Code",
    .print=printCode
};

void initCode(Code* code, int argcount, int nlocals, int stackSize, int flag, String* byteCode, List* consts, List* names, List* varNames, List* freeVars, List* cellVars, String* fileName, String* moduleName, int lineNumber, String* notable){
    initObject(code, &CodeType);
    code->argcount = argcount;
    code->nlocals = nlocals;
    code->stackSize = stackSize;
    code->flag = flag;
    code->byteCode = byteCode;
    code->consts = consts;
    code->names = names;
    code->varNames = varNames;
    code->freeVars = freeVars;
    code->cellVars = cellVars;
    code->fileName = fileName;
    code->moduleName = moduleName;
    code->lineNumber = lineNumber;
    code->notable = notable;
}

void printCode(Object* self) {
    Code* _self = (Code*)self;
    printf("code:\n{\n");
    printf("arg=0x%x  ", _self->argcount);
    printf("nlocals=0x%x   ", _self->nlocals);
    printf("stackSize=0x%x   ", _self->stackSize);
    printf("flag=0x%x\n", _self->flag);
    printf("bytecode: ");
    StringType.print(_self->byteCode);
    printf("\n");
    printf("consts:");
    ListType.print(_self->consts);
    printf("\n");
    printf("names:");
    ListType.print(_self->names);
    printf("\n");
    printf("varnames:");
    ListType.print( _self->varNames);
    printf("\n");
    printf("freeVars");
    ListType.print(_self->freeVars);
    printf("\n");
    printf("cellvars");
    ListType.print( _self->cellVars);
    printf("\n");
    printf("FileName:");
    printString((Object*)_self->fileName);
    printf("\n");
    printf("ModuleName:");
    printString((Object*)_self->moduleName);
    printf("\n");
    printf("lineNumber: %d\n",_self->lineNumber);
    printf("noTable: ");
    printString((Object*)_self->moduleName);
    printf("\n}");
}
